package com.hktf.uilib.db.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hktf.uilib.db.PushDBHelper;
import com.hktf.uilib.db.bean.PushBean;

import java.util.ArrayList;
import java.util.List;

public class PushDao {

	private PushDBHelper pushHelper;

	public PushDao(Context context) {
		pushHelper = new PushDBHelper(context);
	}

	public synchronized void insertPushInfo(PushBean info) {
		SQLiteDatabase database = pushHelper.getWritableDatabase();
		try {

			String sql = "insert into push_info_tb(msgid, title, caption, extreUrl, state, datetime, pushtype,flag) values (?,?,?,?,?,?,?,?)";
			Object[] bindArgs = { info.getMsgid(), info.getTitle(),
					info.getCaption(), info.getExtreUrl(), info.getState(),info.getDatetime(),info.getPushType(),info.getFlag()};
			database.execSQL(sql, bindArgs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != database) {
				database.close();
			}
		}
	}

	/**
	 * 
	 * 获取所有的push信息
	 * 
	 * @return
	 */
	public synchronized List<PushBean> getPushInfoList(String pushType) {
		SQLiteDatabase database = pushHelper.getReadableDatabase();
		List<PushBean> list = new ArrayList<PushBean>();
		Cursor cursor = null;
		try {
			String sql = "select msgid ,title, caption, state, extreUrl, datetime, pushtype, type,flag from push_info_tb order by datetime desc";
			cursor = database.rawQuery(sql, null);
			while (cursor.moveToNext()) {
				PushBean info = new PushBean(cursor.getString(0),
						cursor.getString(1), cursor.getString(2),
						cursor.getString(3), cursor.getString(4),
						cursor.getString(5), cursor.getString(6), cursor.getInt(7));
				info.setFlag(cursor.getString(8));
				if ("news".equals(pushType)) {
					if ("3".equals(info.getPushType())) {
						list.add(info);
					}
				} else if ("case".equals(pushType)) {
					if ("4".equals(info.getPushType())) {
						list.add(info);
					}
				} else {
					list.add(info);
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != database) {
				database.close();
			}
			if (null != cursor) {
				cursor.close();
			}
		}
		return list;
	}

	/**
	 * 删除一个pushinfo
	 */
	public synchronized void deletePushInfo(String msgid) {
		SQLiteDatabase database = pushHelper.getWritableDatabase();
		try {
			database.delete("push_info_tb", "msgid=?", new String[] { msgid });
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != database) {
				database.close();
			}
		}
	}

	/**
	 * 清空所有的info 信息
	 * 
	 * @param lists
	 */
	public synchronized void deleteAllPushInfo(List<PushBean> lists, String pushType) {
		if (lists != null && lists.size() > 0) {
			for (PushBean item : lists) {
				if ("news".equals(pushType)) {
					if ("3".equals(item.getPushType())) {
						deletePushInfo(item.getMsgid());
					}
				} else if ("case".equals(pushType)) {
					if ("4".equals(item.getPushType())) {
						deletePushInfo(item.getMsgid());
					}
				} else {
					deletePushInfo(item.getMsgid());
				}
			}
		}
	}

	/**
	 * 
	 * 更新pushinfo 状态  "0" 未读  "1" 已读
	 * 
	 * @param msgid
	 * @param state
	 */
	public synchronized void updatePushInfo(String msgid, String state) {
		SQLiteDatabase database = pushHelper.getWritableDatabase();
		try {
			String sql = "update push_info_tb set state=? where msgid=?";
			Object[] bindArgs = { state, msgid };
			database.execSQL(sql, bindArgs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != database) {
				database.close();
			}
		}
	}

	/**
	 * 关闭数据库
	 */
	public void closeDB() {
		if (null != pushHelper) {
			pushHelper.close();
		}
	}

}
